package com.jinsi.controller.lv;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.*;
import com.jinsi.service.lv.SyLvCouresClassificationService;
import com.jinsi.service.lv.SyLvCourriculumService;
import com.jinsi.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 吕昊燃
 * @Date 2022/10/12
 * 课程列表数据
 */
@RestController
@RequestMapping("/curriculum")
@Slf4j
public class SyLvCurriculumController {
    @Autowired
    private SyLvCouresClassificationService syLvCouresClassificationService;
    @Autowired
    private SyLvCourriculumService syLvCourriculumService;

    /**
     * 获取课程分类左树数据
     * @return
     */
    @GetMapping("/sort/primary/getAll")
    public R getAllSort(){
        log.info("获取课程分类信息[开始]");
        List<SyLvCourseClassification> getAllSort = syLvCouresClassificationService.getAllSort();
        log.info("获取课程分类信息[结束],getAllSort:{}",getAllSort);
        return R.ok(getAllSort);
    }

    /**
     * 课程表数据
     * 可以根据传进的参数进行模糊查没有参数默认为全查
     * @param syLvCurriculum
     * @return
     */
    @RequestMapping("/primary/getAllCm")
    public R getAllCm(SyLvCurriculum syLvCurriculum) {
        log.info("查看课程表信息[开始],syLvCurriculum:{}",syLvCurriculum);
        List<SyLvCurriculum> getAllCm = syLvCourriculumService.getAllCm(syLvCurriculum);
        Long count = Long.valueOf(syLvCourriculumService.count(null));
        log.info("查看课程表信息[结束],getAllCm:{}",getAllCm);
        return R.custom(0,"成功",getAllCm,count);
    }

    /**
     * 根据传入的参数添加课程分类
     * @param syLvCourseClassification
     * @return
     */
    @PostMapping("/sort/intermediate/add")
    public R add(SyLvCourseClassification syLvCourseClassification){
        log.info("添加课程分类[开始],syLvCourseClassification:{}",syLvCourseClassification);
        syLvCourseClassification.setClassificationCreationTime(new Date());
        SyLvCourseClassification add = syLvCouresClassificationService.add(syLvCourseClassification);
        boolean result = true;
        log.info("添加课程分类[结束],add:{}",add);
        return  R.ok(add);
    }

    /**
     *修改分类名称
     * @param syLvCourseClassification
     * @return
     */
    @RequestMapping("/sort/intermediate/update")
    public R update(SyLvCourseClassification syLvCourseClassification){
        log.info("修改课程分类[开始],syLvCourseClassification:{}",syLvCourseClassification);
        syLvCourseClassification.setClassificationUpdateTime(new Date());
        syLvCouresClassificationService.updateu(syLvCourseClassification);
        boolean result = true;
        log.info("修改课程分类[结束]", result);
        return R.ok("成功");
    }

    /**
     * 删除分类
     * @param classificationId
     * @return
     */
    @RequestMapping("/sort/intermediate/del")
    public R del(int classificationId){
        log.info("删除课程分类[开始],classificationId:{}",classificationId);
        syLvCouresClassificationService.del(classificationId);
        boolean result = true;
        log.info("删除课程分类[结束]", result);
        return R.ok("成功");
    }

    /**
     * 修改上架状态
     * @param curriculumId
     * @param currculumUpdatedBy
     * @return
     */
    @RequestMapping("/intermediate/updateOnShelfStatus")
    public R updateOnShelfStatus(int curriculumId,String currculumUpdatedBy){
        log.info("修改上架状态[开始],curriculumId:{}",curriculumId);
        syLvCourriculumService.updateOnShelfStatus(curriculumId,currculumUpdatedBy);
        boolean result = true;
        log.info("修改上架状态[结束]", result);
        return R.ok("成功");
    }

    /**
     * 下拉框分类查询
     * @return
     */
    @GetMapping("/primary/getAllDropDownBox")
    public R getAllDropDownBox(){
        log.info("分类查询[开始]");
        List<SyLvCoureseNode> getAllDropDownBox = syLvCouresClassificationService.getAllDropDownBox(0);
        boolean result = true;
        log.info("分类查询[结束],getAllDropDownBox:{}", getAllDropDownBox);
        return R.ok(getAllDropDownBox);
    }

    /**
     * 修改课程信息
     * @param syLvCurriculum
     * @param videoDuration
     * @return
     */
    @RequestMapping("/primary/update")
    public R update(SyLvCurriculum syLvCurriculum,Integer videoDuration){
        log.info("修改课程信息[开始],syLvCurriculum:{}",syLvCurriculum);
        Date date=new Date(); //获取一个Date对象
        DateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //创建一个格式化日期对象
        String birthday= simpleDateFormat.format(date); //格式化后的时间

        syLvCurriculum.setCurrculumUpdateTime(birthday);

        syLvCourriculumService.update(syLvCurriculum,videoDuration);
        boolean result = true;
        log.info("修改课程信息[结束]", result);
        return R.ok("成功");
    }

    /**
     * 查询课程日志
     * @param curriculumId
     * @return
     */
    @GetMapping("/primary/log")
    public R log(int curriculumId){
        log.info("查看课程日志[开始],curriculumId:{}",curriculumId);
        List<SylvCurriculumLog> curriculumLog = syLvCourriculumService.log(curriculumId);
        log.info("查看课程日志[结束],curriculumLog:{}", curriculumLog);
        return R.custom(0,"成功",curriculumLog,10l);
    }

    /**
     * 删除课程
     * @param curriculumId
     * @return
     */
    @RequestMapping("/intermediate/delete")
    public R delete(int curriculumId){
        log.info("删除课程[开始],curriculumId:{}",curriculumId);
        syLvCourriculumService.delete(curriculumId);
        boolean result = true;
        log.info("删除课程[结束]", result);
        return  R.ok("成功");
    }

    /**
     * 添加课程
     * @param syLvCurriculum
     *
     * @return
     */
    //int videoDuration,
    @PostMapping("/intermediate/insert")
    public R insert(SyLvCurriculum syLvCurriculum,int videoDuration){
        Date date=new Date(); //获取一个Date对象
        DateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //创建一个格式化日期对象
        String birthday= simpleDateFormat.format(date); //格式化后的时间
        syLvCurriculum.setCurrculumCreationTime(birthday);
        log.info("添加课程[开始],syLvCurriculum:{}",syLvCurriculum);
        syLvCourriculumService.insert(syLvCurriculum,videoDuration);
        boolean result = true;
        log.info("添加课程[结束]", result);
        return  R.ok("成功");
    }

    /**
     * 上传课程视频并进行转码发存给文件服务器
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("intermediate/upload")
    public  R uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        log.info("上传课程视频[开始]: 文件名:{}, 文件大小:{}", file.getName(), file.getSize());
        try {
            Map<String, Object> map = new HashMap<>();
            FastUplodFile fastUplodFile = new FastUplodFile();
            request.getSession().setAttribute("progress",5);
            Map<String, String> map1 = fastUplodFile.uploadFile(file);
            String path = map1.get("path");
            String path1 = map1.get("path1");
            request.getSession().setAttribute("progress",25);
            log.info("上传课程视频,视频上传进度,文件名:{}, 上传进度:{}", file.getName(), request.getSession().getAttribute("progress"));
            String mp4 = VideoUtils.videoConvert(path, path1, 1280, 720, "mp4",request);
            String fileName = mp4;
            String filePath = path1 + "/" + mp4;
            request.getSession().setAttribute("progress",75);
            log.info("上传课程视频,视频上传进度,文件名:{}, 上传进度:{}", file.getName(), request.getSession().getAttribute("progress"));
            //将视频文件转为MultipartFile模式
            MultipartFile file1 = new MockMultipartFile(fileName, new FileInputStream(filePath));
            UploadFile uploadController = new UploadFile();
            //上传视频返回路径
            String s = uploadController.fileUpload(file1);
            File file2 = new File(path1);
            long result = VideoUtils.getDuration(filePath);
            map.put("path",s);
            map.put("h",result);
            request.getSession().setAttribute("file",file2);
            request.getSession().setAttribute("progress",100);
            log.info("上传课程视频[结束]: 上传成功. map:{}", map);
            return R.ok(map);
        } catch (IOException e) {
            log.error("上传视频[失败]", e);
        }

        return R.error();
    }

    /**
     * 时事获取上传进度
     * @param request
     * @return
     */
    @RequestMapping("/intermediate/upload/setInter")
    public R setInterval(HttpServletRequest request){
        log.info("获取进度[开始]");
       Object setInter =   request.getSession().getAttribute("progress");
        log.info("获取进度[结束],setInter:{}", setInter);
        return R.ok(String.valueOf(setInter));
    }
    /**
     * 删除本地视频
     * @param request
     * @return
     */
    @RequestMapping("/intermediate/upload/deleteVideo")
    public R deleteVideo(HttpServletRequest request){
        log.info("删除本地视频[开始]");
        Object file =   request.getSession().getAttribute("file");
        HttpSession session = request.getSession();

        session.removeAttribute("progress");
        FileDelete fileDelete = new FileDelete();

        fileDelete.delete((File) file);
        log.info("删除本地视频[结束]");
        return R.ok();
    }
    /**
     *根据用户id查看审核详情
     * @param curriculumId
     * @return
     */
    @GetMapping("/primary/examineLog")
    public  R examinelog(int curriculumId){
        Map<String, Object> map = new HashMap<>();
        log.info("查看审核详情[开始],curriculumId:{}",curriculumId);
        List<SyLvCurriculumExamineLog> examinelog = syLvCourriculumService.examineLog(curriculumId);
        log.info("查看审核详情[结束],examinelog:{}", examinelog);
        return R.custom(0,"成功",examinelog,10l);
    }

    /**
     * 管理员课程审核提交
     * @param curriculumId
     * @param curriculumToExamine
     * @param logAuditDetails
     * @param uname
     * @return
     */
    @PostMapping("/intermediate/auditDetails")
    public R auditDetails(int curriculumId,int curriculumToExamine,String logAuditDetails,String uname){
        log.info("审核提交[开始],curriculumId:{}",curriculumId);
        syLvCourriculumService.auditDetails(curriculumId,curriculumToExamine,logAuditDetails,uname);
        boolean result = true;
        log.info("添加课程[结束]", result);
        return R.ok("成功");
    }

    /**
     *查看审核日志
     * @return
     */
    @GetMapping("/intermediate/auditLog")
    public R auditLog(){
        log.info("查看审核日志[开始]");
        List<SyLvCurriculumExamineLog> syLvCurriculumExamineLogs = syLvCourriculumService.auditLog();
        log.info("查看审核日志[结束],syLvCurriculumExamineLogs:{}",syLvCurriculumExamineLogs);
        return R.custom(0,"成功",syLvCurriculumExamineLogs,10l);
    }

    /**
     * 日志操作记录
     * @param syLvOperationLog
     * @return
     */
    @PostMapping("/intermediate/addOperationLog")
    public R addoperationlog(SyLvOperationLog syLvOperationLog){
        log.info("记录日志操作[开始],syLvOperationLog:{}",syLvOperationLog);
        syLvCourriculumService.addOperationLog(syLvOperationLog);
        boolean result = true;
        log.info("记录日志操作[结束]", result);
        return R.ok("成功");
    }

    /**
     * 查看操作记录
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/intermediate/operationLog")
    public R operationLog(int page,int limit){
        log.info("查看操作记录[开始]");
        List<SyLvOperationLog> operationLog = syLvCourriculumService.operationLog();
        PageHelper.startPage(page,limit);
        PageInfo<SyLvOperationLog> pageInfo = new PageInfo<>(operationLog);
        log.info("查看操作记录[结束],operationLog:{}",operationLog);
        return R.custom(0,"成功",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 删除日志操作记录
     * @param LogId
     * @return
     */
    @RequestMapping("/intermediate/deloperationLog")
    public R deloperationLog(int LogId){

        log.info("查看操作记录[开始],LogId:{}",LogId);
        syLvCourriculumService.deloperationLog(LogId);
        boolean result = true;
        log.info("记录日志操作[结束]", result);
        return R.ok("成功");
    }
}
